package edu.drexel.jrexel2d.input;

import java.util.List;
import java.util.Map;

import edu.drexel.jrexel2d.camera.Camera;
import edu.drexel.jrexel2d.gamestate.Level;
import edu.drexel.jrexel2d.objects2D.Player;

/**
 * @author Chris
 * 
 */
public class InputEngine {

	/**
	 * This should be used with the client
	 * 
	 * @param curLevel
	 * @param list
	 */
	public void processGameActions(Level curLevel,
			List<GameAction> gameActions, Long time) {
		List<Player> players = curLevel.getPlayers();
		for (GameAction ga : gameActions) {
			ga.setTime(time);
			for (Player player : players) {
				if (ga.getCharacterId() == player.getPlayerId()) {
					executeGameAction(ga, player);

				}

			}
		}
	}

	//this is a test to see if the camera works better updated ever game action
	public void processGameActions(Level curLevel,
			List<GameAction> gameActions, Long time, Camera camera,
			int displayWidth, int displayHeight) {
		List<Player> players = curLevel.getPlayers();
		for (GameAction ga : gameActions) {
			ga.setTime(time);
			for (Player player : players) {
				if (ga.getCharacterId() == player.getPlayerId()) {
					executeGameAction(ga, player);
					camera.updateCamera(curLevel, displayWidth, displayHeight);

				}

			}
		}
	}

	public void clearGameActions(InputMap inputMap, List<GameAction> gameActions) {
		gameActions.clear(); // after actions are rendered, clear the list.
		inputMap.getGameInputObject().getGameActions().clear();// clear action
																// list
	}

	/**
	 * This should only be used by the server
	 * 
	 * @param curLevel
	 * @param gameActions
	 */
	public void processGameActions(Level curLevel,
			List<GameAction> gameActions, Map<Integer, Long> timeOfPlayerUpdates) {
		// this code takes the user input and updates the position of the
		// player.
		// TODO: this code needs a better implementation
		List<Player> players = curLevel.getPlayers();
		for (GameAction ga : gameActions) {
			for (Player player : players) {
				if (ga.getCharacterId() == player.getPlayerId()) {
					executeGameAction(ga, player);
					timeOfPlayerUpdates.put(player.getPlayerId(), ga.getTime());

				}

			}
		}
	}

	public void executeGameAction(GameAction ga, Player player) {
		if (ga.getAction() == CharacterActions.MOVE_UP) {
			// System.out.println("player: " +
			// player.getPlayerId()+" Moving up");
			player.moveUp();
		} else if (ga.getAction() == CharacterActions.MOVE_DOWN) {
			// System.out.println("player: " +
			// player.getPlayerId()+" Moving down");
			player.moveDown();
		} else if (ga.getAction() == CharacterActions.MOVE_LEFT) {
			// System.out.println("player: " +
			// player.getPlayerId()+" Moving left");
			player.moveLeft();
		} else if (ga.getAction() == CharacterActions.MOVE_RIGHT) {
			// System.out.println("player: " +
			// player.getPlayerId()+" Moving right");
			player.moveRight();
		} else if (ga.getAction() == CharacterActions.JUMP) {
			// System.out.println("player: " +
			// player.getPlayerId()+" Moving jump");
			player.jump();
		} else if (ga.getAction() == CharacterActions.PICK_UP){
	        player.pickup(); 
	    } else if (ga.getAction() == CharacterActions.THROW){
	        player.throwObject(); 
	    } else if (ga.getAction() == CharacterActions.ROLL){
	        player.rollObject(); 
	    }
	}
}
